home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / FREQ.TST / SETCHISQ.C < prev    next >
C/C++ Source or Header  |  1996-01-10  |  3KB  |  103 lines

  1. /* ============ */
  2. /* setchisq.c    */
  3. /* ============ */
  4. #include <io.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <mconf.h>
  9. #include <miscdefs.h>
  10. #include <freqdefs.h>
  11.  
  12. #define    ACT(X)    #X
  13.  
  14. #define    CLAMP(Out, Var, Lo, Hi)    Out = __min(Hi, __max(Lo, Var))
  15.  
  16. #define    NEED_ALL(Label)    "\nEnter "Label
  17.  
  18. #define    NEED_USER_ENTRY(LABEL, LO, HI) \
  19.     NEED_ALL(LABEL" ["ACT(LO)"-"ACT(HI)"]: ")
  20.  
  21. #define    ELEMS_LABEL    "Number of Categories in Sample Space"
  22. #define    EXPECT_LABEL    "Expected No. Events for Each Category"
  23.  
  24. #define    REPORT_USER_INT_ENTRY(Entry, Label)        \
  25.     {                            \
  26.     fflush(NULL); printf("\n");            \
  27.     printf("\tNumber Entered:  %.f", (double)Entry);\
  28.     printf(" (%s)\n", Label);            \
  29.     }
  30. #define    SET_TWO_POW(Val)                \
  31.     {                            \
  32.     UINT    TwoPow = MIN_ELEMS;            \
  33.     while (TwoPow < Val)                \
  34.     {                        \
  35.         TwoPow <<= 1;                \
  36.     }                        \
  37.     if (Val < (TwoPow - (TwoPow >> 2)))         \
  38.     {                        \
  39.         Val = TwoPow >> 1;                \
  40.     }                        \
  41.     else                        \
  42.     {                        \
  43.         Val = TwoPow;                \
  44.     }                        \
  45.     }
  46. #define    SHOW_INT_VALUE_USED(Entered, Used)             \
  47.     printf("\tTest Value Used: %.f%s\n", (double)Used,    \
  48.     ((double)Entered == (double)Used) ? "" : " (Clamped)")
  49.  
  50. /* ==================================================================== */
  51. /* SetChiSqControls - Puts Run-Control Parameters in CHISQ_STRU        */
  52. /* ==================================================================== */
  53. void
  54. SetChiSqControls(CHISQ_STRU *ChiSqData)
  55. {
  56.     int     NewlineCh;
  57.     int     UserIntEntry;
  58.     UINT    UserUintEntry;
  59.  
  60.     NewlineCh = _isatty(_fileno(stdin)) ? '\r' : '\n';
  61.  
  62.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  63.     /* ------------------------------------------------------------- */
  64.     /* Get Number of Elements in Sample Space (Number of Categories) */
  65.     /* ------------------------------------------------------------- */
  66.     GetUint(NEED_USER_ENTRY(ELEMS_LABEL, MIN_ELEMS, MAX_ELEMS),
  67.     &UserUintEntry);
  68.  
  69.     REPORT_USER_INT_ENTRY(UserUintEntry, ELEMS_LABEL);
  70.  
  71.     CLAMP(ChiSqData->NumElements, UserUintEntry,
  72.     (UINT)MIN_ELEMS, (UINT)MAX_ELEMS);
  73.  
  74.     /* ---------------------------------------- */
  75.     /* Get Nearest Power of Two to User's Input */
  76.     /* ---------------------------------------- */
  77.     SET_TWO_POW(ChiSqData->NumElements);
  78.     SHOW_INT_VALUE_USED(UserUintEntry, ChiSqData->NumElements);
  79.  
  80.    /* ------------------------ */
  81.    /* Set Category Expectation */
  82.    /* ------------------------ */
  83.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  84.     GetInt(NEED_USER_ENTRY(EXPECT_LABEL, MIN_EXPECT, MAX_EXPECT),
  85.     &UserIntEntry);
  86.  
  87.     REPORT_USER_INT_ENTRY(UserIntEntry, EXPECT_LABEL);
  88.  
  89.     CLAMP(ChiSqData->CellExpect, UserIntEntry, MIN_EXPECT, MAX_EXPECT);
  90.  
  91.     SHOW_INT_VALUE_USED(UserIntEntry, ChiSqData->CellExpect);
  92.  
  93.     /* ------------------------------------------------------------ */
  94.     /* Calculate Number of Variates Required to Achieve Expectation */
  95.     /* ------------------------------------------------------------ */
  96.     ChiSqData->NumVariates =
  97.     ((ULONG)ChiSqData->CellExpect * (ULONG)ChiSqData->NumElements);
  98.  
  99.     P(printf("NumVariates = %.f\n", (double)ChiSqData->NumVariates));
  100.  
  101.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  102. }
  103.